package com.kabuda.blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.kabuda.blog.service.CommentsService;
import com.kabuda.blog.service.SysUserService;
import com.kabuda.blog.vo.CommentVo;
import com.kabuda.blog.vo.Result;
import com.kabuda.blog.dao.mapper.CommentsMapper;
import com.kabuda.blog.dao.pojo.Comment;
import com.kabuda.blog.dao.pojo.SysUser;
import com.kabuda.blog.utils.UserThreadLocal;
import com.kabuda.blog.vo.UserVo;
import com.kabuda.blog.vo.params.CommentParam;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author kabuda
 * @create 2023-01-18 14:50
 */
@Service
public class CommentServiceImpl implements CommentsService {
    @Resource
    private CommentsMapper CommentMapper;
    @Resource
    private SysUserService sysUserService;

    @Override
    public Result getArticleComments(String id) {
        LambdaQueryWrapper<Comment> queryWrapper = new LambdaQueryWrapper<Comment>();
        queryWrapper.eq(Comment::getArticleId, id);
        //一级评论
        queryWrapper.eq(Comment::getLevel, 1);
        List<Comment> CommentList = CommentMapper.selectList(queryWrapper);
        List<CommentVo> commentVos = copyList(CommentList);
        return Result.success(commentVos);
    }

    @Override
    public Result comment(CommentParam commentParam) {
        SysUser sysUser = UserThreadLocal.get();
        Comment comment = new Comment();
        comment.setArticleId(commentParam.getArticleId());
        comment.setAuthorId(sysUser.getId());
        comment.setContent(commentParam.getContent());
        comment.setCreateDate(System.currentTimeMillis());
        Long parent = commentParam.getParent();
        if (parent == null || parent == 0) {
            comment.setLevel(1);
        }else{
            comment.setLevel(2);
        }
        comment.setParentId(parent == null ? 0 : parent);
        Long toUserId = commentParam.getToUserId();
        comment.setToUid(toUserId == null ? 0 : toUserId);
        this.CommentMapper.insert(comment);
        return Result.success(null);
    }

    private List<CommentVo> copyList(List<Comment> CommentList) {
        List<CommentVo> commentVos = new ArrayList<>();
        for (Comment comment : CommentList) {
            commentVos.add(copy(comment));
        }
        return commentVos;
    }

    private CommentVo copy(Comment comment) {
        CommentVo commentVo = new CommentVo();
        BeanUtils.copyProperties(comment, commentVo);

        //子评论
        if (comment.getLevel() == 1) {
            List<CommentVo> commentVoList = this.findCommentsByParentId(comment.getId());
            commentVo.setChildrens(commentVoList);

        }
        //Author信息
        UserVo author = sysUserService.findUserVoById(comment.getAuthorId());
        commentVo.setAuthor(author);

        //给谁评论
        if (comment.getLevel()==2) {
            UserVo toUser = sysUserService.findUserVoById(comment.getToUid());
            commentVo.setToUser(toUser);
        }

        return commentVo;
    }

    private List<CommentVo> findCommentsByParentId(Long id) {
        LambdaQueryWrapper<Comment> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Comment::getParentId,id);
        queryWrapper.eq(Comment::getLevel,2);
        List<Comment> comments = this.CommentMapper.selectList(queryWrapper);
        //递归查询
        return copyList(comments);
    }

}



